/**
 * 
 */
package unseen.games.renderer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import unseen.games.model.Wave;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.view.Display;
import android.view.MotionEvent;

/**
 * @author PERRAUDIN Steven <sperraudin@neotilus.com>
 *
 * @date 4 juin 2010
 */
public class GenericRenderer implements GLSurfaceView.Renderer{

	private float _red 		= 0f;
    private float _green 	= 0f;
    private float _blue 	= 0f;
	
    private float _angle;
    
    private List<Wave> _waves;
    private List<Wave> _wavesSafe;
    private List<Wave> _waveCopy;
    
    private Wave _wave;
    
    private int _width;
    private int _height;
    
    private Random r;
    
	public GenericRenderer(int width, int height){
		_waves 		= new ArrayList<Wave>();
		_waveCopy 	= new ArrayList<Wave>();
		r			= new Random();
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		
		// define the color we want to be displayed as the "clipping wall"
        gl.glClearColor(_red, _green, _blue, 200.0f);
        
        // reset the matrix - good to fix the rotation to a static angle
        gl.glLoadIdentity();
        
        // clear the color buffer to show the ClearColor we called above...
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        
        // set rotation
        gl.glRotatef(_angle, 0f, 1f, 0f);

        _wavesSafe 	= Collections.synchronizedList(_waves);
        _waveCopy.clear();
        _waveCopy.addAll(_wavesSafe);
        
        synchronized(_waveCopy){
        
	        for(Wave w : _waveCopy){
	        	
	        	if(w.radius < 20){
		        	gl.glColor4f(w.rgb[0], w.rgb[1], w.rgb[2], 0.0f);
		            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, w.getVertexBuffer());
//		            gl.glDrawElements(GL10.GL_TRIANGLES, w.getNrOfVertices(), GL10.GL_UNSIGNED_SHORT, w.getIndexBuffer());
		            gl.glDrawElements(GL10.GL_TRIANGLE_FAN, w.getNrOfVertices(), GL10.GL_UNSIGNED_SHORT, w.getIndexBuffer());

		            w.animate();
	        	}
	        	else{
	        		_waves.remove(w);
	        	}
	        }
        }
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		
		gl.glMatrixMode(GL10.GL_PROJECTION);
        GLU.gluOrtho2D(gl, 0, _width, _height, 0);
        
        gl.glMatrixMode( GL10.GL_MODELVIEW );
        
	}
	
	public void addWave(MotionEvent event) {
		_wave 	= new Wave(event.getX(), event.getY(), r.nextFloat(), r.nextFloat(), r.nextFloat());
		_waves.add(_wave);		
	}

	public void setDisplay(Display display) {
		_waves 	= new ArrayList<Wave>();
		
		_width 	= display.getWidth();
		_height = display.getHeight();
		
	}	

}
